<?php 
define("YSI_VERSION", "0.1.0");

class YSI {

    public $resourceBase;
    public $template;
    public function __construct($resourceBase, $template) {
        $this->resourceBase = $resourceBase;
        $this->template = $template;
    }
    public function load() {
        define("YSI_RESOURCE_BASE", $this->resourceBase);
        require_once ($this->template);
    }
    
    public function version($version = "0.0.0") {
        $versionParts = explode(".", $version);
        $YSIVersionParts = explode(".", YSI_VERSION);
        
        $result = true;
        
        for ($n = 0, $l = count($YSIVersionParts); $n < $l; $n++) {
            if ($versionParts[$n] == "*") {
                break;
            } else {
                if ($versionParts[$n] != $YSIVersionParts[$n]) {
                    $result = false;
                    break;
                }
            }
        }
        
        return $result;
    }
    
}

/**
 *
 * @return string
 * @param array $params[optional]
 * 		"id"=>"the dom-id of the panel".
 * 		"class"=>"the extend className of the panel". values: ["ysi-panel-main","ysi-panel-normal","ysi-panel-extra"]
 *
 */
function ysi_panel_start($params = array(),$type = null) {
	if(!$type){
		return _ysi_excute(ysit_panel_start, $params);
	}else{
		switch($type){
			case "gradient": $params['class'] .= " ysi-panel-gradient";
				return _ysi_excute(ysit_panel_gradient_start, $params);
				break;
			case "square": $params['class'] .= " ysi-panel-square";
				return _ysi_excute(ysit_panel_square_start, $params);
				break;
			case "independent": $params['class'] .= " ysi-panel-independent";
				return _ysi_excute(ysit_panel_square_start, $params);
				break;
			case "independent-border": $params['class'] .= " ysi-panel-independent-border";
				return _ysi_excute(ysit_panel_border_start, $params);
				break;
		}
	}
}

function ysi_panel_end($type= null) {
	if(!$type){
    return _ysi_excute(ysit_panel_end, $params);
	}else{
		switch($type){
			case "gradient": return _ysi_excute(ysit_panel_gradient_end, $params);
				break;
			case "square": return _ysi_excute(ysit_panel_square_end, $params);
				break;
			case "independent": return _ysi_excute(ysit_panel_square_end, $params);
				break;
			case "independent-border": return _ysi_excute(ysit_panel_border_end, $params);
				break;
		}
	}
}

function ysi_image($params = array("class"=>"ysi-image-normal")) {
    return _ysi_excute(ysit_image, $params);
}

function ysi_input_text($params = array()) {
    if ($params['error'] && $params['error'] != "") {
        $params['infoHidden'] = "hidden";
    }
    return _ysi_excute(ysit_input_text, $params);
}

function ysi_input_password($params = array()) {
    return _ysi_excute(ysit_input_password, $params);
}

function ysi_input_submit($params = array()) {
    return _ysi_excute(ysit_input_submit, $params);
}

function ysi_input_button($params = array()) {
    return _ysi_excute(ysit_input_button, $params);
}

function ysi_input_file($params = array()) {
    return _ysi_excute(ysit_input_file, $params);
}

function ysi_input_checkbox($params = array()) {
    return _ysi_excute(ysit_input_checkbox, $params);
}

function ysi_input_radio($params = array()) {
    return _ysi_excute(ysit_input_radio, $params);
}

function ysi_select($params = array()) {
    return _ysi_excute(ysit_input_select, $params);
}

function ysi_ckeditor($params = array()) {
    return _ysi_excute(ysit_ckeditor, $params);
}

function ysi_textarea($params = array()) {
    return _ysi_excute(ysit_input_textarea, $params);
}

function ysi_datepicker($params = array()) {
    return _ysi_excute(ysit_datepicker, $params);
}

function ysi_vspace($params = array("class"=>"ysi-vspace-normal")) {
    return _ysi_excute(ysit_vspace, $params);
}

function ysi_breadcrumb($params) {
    if (!isset($params['separator'])) {
        $params['separator'] = ' &raquo; ';
    }
    
    $links = array();
    $numSteps = count($params['items']);
    for ($i = 0; $i < $numSteps; $i++) {
        $step = $params['items'][$i];
				
				$max = $params['full'] ? $numSteps : $numSteps -1;
        
        if (strlen($step['href']) > 0 && $i < $max) {
            $links[] = sprintf('<a href="%s" title="%s">%s</a>', htmlSpecialChars($step['href']),
                 htmlSpecialChars($step['title']), htmlSpecialChars($step['title']));
        } else {
            $links[] = htmlSpecialChars($step['title']);
        }
    }
    
    $html = join($params['separator'], $links);
    
    return $html;
}

function _ysi_excute($template = "", $params = array()) {
    global $_params;
    $_params = $params;
    
    $html = preg_replace_callback("/(\\$\{([a-zA-Z0-9]+)\}|\\$\\$\{([a-zA-Z0-9]+)\})/", '_ysi_excute_callback',
         $template);
    $html = preg_replace(array("/[a-zA-Z]+\=\"\"/", "/\n/"), array("", ""), $html);
    return $html."\n";
}

function _ysi_excute_callback($matches) {
    global $_params;
    if (strpos($matches[0], "$$") === 0) {
        $temp = $_params[$matches[3]] ? $_params[$matches[3]] : "";
    } else {
        $temp = $_params[$matches[2]] ? preg_replace(array("/\"/"), array("\\\""), $_params[$matches[2]]) : "";
    }
    return $temp;
}
?>
